package com.medicinemanager.ui;

import com.example.medicinemanager.R;
import com.medicinemanager.app.MedicineApp;
import com.medicinemanager.bean.Entity;
import com.medicinemanager.business.InitMedicineInformation;
import com.medicinemanager.business.ShowMedicineSource;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class ManagerMedicineActivity extends BaseActivity{
	private ListView mListView = null;
	private final int RESOURCE = android.R.layout.simple_list_item_1;
	private MedicineApp mMedicineApp = null;
	private Button mBtnAdd = null;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.data_view_activity);
		
		initAction();
		initButton();
	}
	
	private void initButton(){
		mBtnAdd = (Button) findViewById(R.id.btn_add);
		mBtnAdd.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(ManagerMedicineActivity.this, MedicineAdd.class);
				ManagerMedicineActivity.this.startActivity(intent);
			}
		});
	}
	
	public void onResume(){
		initView();
		super.onResume();
	}
	
	private void initView(){
		mListView = (ListView) findViewById(R.id.lv_showdata);
		ArrayAdapter<Entity> arrayAdapter = new ArrayAdapter<Entity>(this, RESOURCE, mMedicineApp.getMedicines());
		mListView.setAdapter(arrayAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.putExtra("medicine", arg2);
				intent.setClass(ManagerMedicineActivity.this, ShowInformationActivity.class);
				ManagerMedicineActivity.this.startActivity(intent);
			}
		});
	}
	
	private void initAction(){
		mMedicineApp = (MedicineApp) getApplication();
		new InitMedicineInformation(ManagerMedicineActivity.this);
		new ShowMedicineSource(this).parseData();
	}
	@Override
	public void gotoBack() {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		intent.setClass(ManagerMedicineActivity.this, MainActivity.class);
		this.startActivity(intent);
	}

	@Override
	public void gotoAhead() {
		// TODO Auto-generated method stub
		
	}

}
